# import protobuf required by onnx proto
if(NOT TARGET libprotobuf)
    FetchContent_GetProperties(protobuf)
    if(NOT protobuf_POPULATED)
        if(CMAKE_COMPILER_IS_GNUCC)
            if(CMAKE_CXX_FLAGS MATCHES "-Werror=non-virtual-dtor")
                set(__err_non_virtual_dtor__ True)
                string(REPLACE "-Werror=non-virtual-dtor" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
            else()
                set(__err_non_virtual_dtor__ False)
            endif()
        endif()

        FetchContent_Populate(protobuf)
        if(EXISTS ${protobuf_SOURCE_DIR}/CMakeLists.txt)
            add_subdirectory(${protobuf_SOURCE_DIR} ${protobuf_BINARY_DIR})
        elseif(EXISTS ${protobuf_SOURCE_DIR}/cmake/CMakeLists.txt)
            add_subdirectory(${protobuf_SOURCE_DIR}/cmake ${protobuf_BINARY_DIR})
        endif()

        if(CMAKE_COMPILER_IS_GNUCC)
            if(__err_non_virtual_dtor__)
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=non-virtual-dtor")
            endif()

            set(__PPLNN_PROTOBUF_COMPILE_FLAGS__ "-Wno-sign-compare -Wno-unused-variable -Wno-unused-function -Wno-uninitialized")
            if(TARGET libprotobuf)
                set_target_properties(libprotobuf PROPERTIES COMPILE_FLAGS ${__PPLNN_PROTOBUF_COMPILE_FLAGS__})
            endif()
            if(TARGET libprotobuf-lite)
                set_target_properties(libprotobuf-lite PROPERTIES COMPILE_FLAGS ${__PPLNN_PROTOBUF_COMPILE_FLAGS__})
            endif()
            if(TARGET protoc)
                set_target_properties(protoc PROPERTIES COMPILE_FLAGS ${__PPLNN_PROTOBUF_COMPILE_FLAGS__})
            endif()
            if(TARGET libprotoc)
                set_target_properties(libprotoc PROPERTIES COMPILE_FLAGS ${__PPLNN_PROTOBUF_COMPILE_FLAGS__})
            endif()
            unset(__PPLNN_PROTOBUF_COMPILE_FLAGS__)
        endif()

        unset(__err_non_virtual_dtor__)
    endif()
endif()
